<?php
/**
 * Created by PhpStorm.
 * User: Andy
 * Date: 2019/3/13
 * Time: 14:43
 */

namespace App\api\controller;
use Common\controller\Controller;
use Common\helper\AesHelper;
use Curl;


/**
 * 富友富保保平台 接口类
 * Class FuyouInsurance
 * @package App\api\controller
 */
class FuyouInsurance extends Controller
{
    private $createOrderUrl='https://wxtest-fn.fuiou.com/api/apiTakeOutOrder.do';//下单接口
    private $queryOrderUrl='https://wxtest-fn.fuiou.com/api/apiQuery.do'; //查询订单接口
    private $md5key='cc022cbea29423ed070143c7eacf6303';
    private $aesKey='b4246eeeff4f48ea';


    /**
     * 创建订单
     * @throws \ErrorException
     */
    public function createOrder(){
        //顺序有关，不可乱序
        $dataSign=[
            $_GET['firmNo'],
            $_GET['loginId'],
            $_GET['customerType'],
            $_GET['serialNumber'],
            $_GET['projectCode'],
            $_GET['insurerName'],
            $_GET['insurerDocNo'],
            $_GET['insurerDocType'],
            $_GET['insurerPhoneNo'],
            $this->md5key
        ];

        $dataSign=implode('|',$dataSign);
        $sign=md5($dataSign);

        $data=[
            'loginId'=>$_GET['loginId'],
            'customerType'=>$_GET['customerType'],
            'serialNumber'=>$_GET['serialNumber'],
            'projectCode'=>$_GET['projectCode'],
            'startTime'=>$_GET['startTime'],
            'insurerName'=>$_GET['insurerName'],
            'insurerDocNo'=>$_GET['insurerDocNo'],
            'insurerDocType'=>$_GET['insurerDocType'],
            'insurerPhoneNo'=>$_GET['insurerPhoneNo'],
            'insurerEmail'=>$_GET['insurerEmail'],
            'insInsurerName'=>$_GET['insInsurerName'],
            'insInsurerDocNo'=>$_GET['insInsurerDocNo'],
            'insInsurerDocType'=>$_GET['insInsurerDocType'],
            'insInsurerEmail'=>$_GET['insInsurerEmail'],
            'insInsurerPhoneNo'=>$_GET['insInsurerPhoneNo'],
            'reserved1'=>$_GET['reserved1'],
            'reserved2'=>$_GET['reserved2'],
        ];

        $key=$this->aesKey;
        $data=array_map(function ($v){
            return mb_convert_encoding($v,'UTF-8');
        },$data);

        $dataAes=array_map(function ($v) use($key){
            return (new AesHelper($key))->encrypt($v);
        },$data);

        $urlSuffix=http_build_query(['firmNo'=>$_GET['firmNo'],]+$dataAes+['sign'=>$sign,]);//注意替换值
        $url=$this->createOrderUrl.'?'.$urlSuffix;

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->get($url);

        if ($curl->error) {
            echo $curl->error_code;
        }
        else {
            echo $curl->response;
        }


    }

    /**
     * 查询订单
     */
    public function queryOrder(){
        $dataSign=[
            $_GET['firmNo'],
            $_GET['loginId'],
            $_GET['serialNumber'],
            $this->md5key,
        ];

        $dataSign=implode('|',$dataSign);
        $sign=md5($dataSign);

        $data=[
            'loginId'=>$_GET['loginId'],
            'serialNumber'=>$_GET['serialNumber'],
        ];

        $key=$this->aesKey;
        $data=array_map(function ($v){
            return mb_convert_encoding($v,'UTF-8');
        },$data);

        $dataAes=array_map(function ($v) use($key){
            return (new AesHelper($key))->encrypt($v);
        },$data);

        $urlSuffix=http_build_query(['firmNo'=>$_GET['firmNo']]+$dataAes+['sign'=>$sign]);//注意替换值
        $url=$this->queryOrderUrl.'?'.$urlSuffix;

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->get($url);
        if ($curl->error) {
            echo $curl->error_code;
        }
        else {
            echo $curl->response;
        }


    }


}